package com.datalayer.testclasses;

import com.datalayer.annotations.*;

import java.util.Collection;
import java.util.LinkedList;

import static com.datalayer.utils.AssociationType.MANY2ONE;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 7/6/12
 * Time: 4:30 PM
 */
@SuppressWarnings("ALL")
@Entity(tableName = "Departament")
public class Departament {

    int id;
    String nume;
    Collection<Angajat> angajati = new LinkedList<Angajat>();

    public Departament() {
    }

    public Departament(int id, String nume) {
        this.id = id;
        this.nume = nume;
    }

    @Direction("get")
    @Association(type = MANY2ONE, joins = {
            @Join(fieldName = "angajati", foreignName = "departament_fk", localAttribute = "id"),
            @Join(fieldName = "angajati", foreignName = "dep_name_fk", localAttribute = "nume")})
    public Collection<Angajat> getAngajati() {
        return angajati;
    }

    @Direction("get")
    @Constraint(constraintName = "primary key")
    @Attribute(fieldName = "id", attributeName = "id_dep", type = "int")
    public int getId() {
        return id;
    }

    @Direction("get")
    @Constraint(constraintName = "primary key")
    @Attribute(fieldName = "nume", attributeName = "nume_dep", type = "varchar(20)")
    public String getNume() {
        return nume;
    }

    @Direction("set")
    @Association(type = MANY2ONE, joins = {
            @Join(fieldName = "angajati", insideName = "id", foreignName = "departament_fk"),
            @Join(fieldName = "angajati", insideName = "nume", foreignName = "dep_name_fk")})
    public void setAngajati(LinkedList<Angajat> angajati) {
        this.angajati = angajati;
    }

    @Direction("add")
    @Association(type = MANY2ONE, joins = {
            @Join(fieldName = "angajati", insideName = "id", foreignName = "departament_fk"),
            @Join(fieldName = "angajati", insideName = "nume", foreignName = "dep_name_fk")})
    public void addAngajati(Angajat angajat) {
        angajati.add(angajat);
    }

    @Direction("set")
    @Attribute(fieldName = "id", attributeName = "id_dep", type = "int")
    public void setId(int id) {
        this.id = id;
    }

    @Direction("set")
    @Attribute(fieldName = "nume", attributeName = "nume_dep", type = "varchar(20)")
    public void setNume(String nume) {
        this.nume = nume;
    }

    @Override
    public String toString() {
        return "Departament{" +
                "angajati=" + angajati +
                ", id=" + id +
                ", nume='" + nume + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Departament that = (Departament) o;

        if (id != that.id) return false;
        if (nume != null ? !nume.equals(that.nume) : that.nume != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (nume != null ? nume.hashCode() : 0);
        return result;
    }
}
